// package com.hu.auth.common.security;
//
// import com.fasterxml.jackson.databind.ObjectMapper;
// import com.hu.auth.common.result.R;
// import com.hu.auth.common.result.RCodeEnum;
// import com.hu.auth.common.utils.JwtUtil;
// import com.hu.auth.common.utils.ResponseUtil;
// import com.hu.auth.model.po.LoginPo;
// import org.springframework.security.authentication.AuthenticationManager;
// import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
// import org.springframework.security.core.Authentication;
// import org.springframework.security.core.AuthenticationException;
// import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
// import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
//
// import javax.servlet.FilterChain;
// import javax.servlet.http.HttpServletRequest;
// import javax.servlet.http.HttpServletResponse;
// import java.io.IOException;
// import java.util.HashMap;
// import java.util.Map;
//
// public class LoginFilter extends UsernamePasswordAuthenticationFilter {
//
//     // @Resource
//     // LoginLogService loginLogService;
//
//     public LoginFilter(AuthenticationManager authenticationManager) {
//         this.setAuthenticationManager(authenticationManager);
//         this.setPostOnly(false);
//         this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/user/login", "POST"));
//     }
//
//     @Override
//     public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//         try {
//             LoginPo loginPo = new ObjectMapper().readValue(request.getInputStream(), LoginPo.class);
//             Authentication authenticationToken = new UsernamePasswordAuthenticationToken(loginPo.getUsername(), loginPo.getPassword());
//             return this.getAuthenticationManager().authenticate(authenticationToken);
//         } catch (IOException e) {
//             e.printStackTrace();
//         }
//         return null;
//     }
//
//
//     //认证成功调用
//     @Override
//     protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
//                                             FilterChain chain, Authentication auth) {
//         //获取认证对象
//         SecurityUser securityUser = (SecurityUser) auth.getPrincipal();
//
//         //保存权限数据
//         // redisTemplate.opsForValue().set(securityUser.getUsername(),
//         //         JSON.toJSONString(securityUser.getAuthorities()));
//
//         //生成token
//         String token = JwtUtil.createToken(securityUser.getUser().getId(), securityUser.getUser().getUsername());
//
//         //记录登录日志
//         // loginLogService.recordLoginLog(securityUser.getUsername(), 1,
//         //         IpUtil.getIpAddress(request), "登录成功");
//
//         //返回
//         Map<String, Object> map = new HashMap<>();
//         map.put("token", token);
//         ResponseUtil.out(response, R.ok(map));
//     }
//
//     //认证失败
//     @Override
//     protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
//         if (e.getCause() instanceof RuntimeException) {
//             ResponseUtil.out(response, R.data(null, 204, e.getMessage()));
//         } else {
//             ResponseUtil.out(response, R.data(null, RCodeEnum.LOGIN_MOBLE_ERROR));
//         }
//     }
//
// }
